<template>
  <div class="cart-container">
    <el-card v-if="cartStore.items.length > 0">
      <template #header>
        <div class="cart-header">
          <h2>我的购物车</h2>
          <el-button type="danger" @click="clearCart">清空购物车</el-button>
        </div>
      </template>
      
      <el-table :data="cartStore.items" style="width: 100%">
        <el-table-column label="商品" min-width="400">
          <template #default="{ row }">
            <div class="product-info">
              <img :src="row.image" class="product-image" />
              <div class="product-details">
                <h3>{{ row.name }}</h3>
                <p class="product-description">{{ row.description }}</p>
              </div>
            </div>
          </template>
        </el-table-column>
        
        <el-table-column label="单价" width="120">
          <template #default="{ row }">
            ¥{{ row.price }}
          </template>
        </el-table-column>
        
        <el-table-column label="数量" width="200">
          <template #default="{ row }">
            <el-input-number
              v-model="row.quantity"
              :min="1"
              :max="row.stock"
              @change="(value) => updateQuantity(row.id, value)"
            />
          </template>
        </el-table-column>
        
        <el-table-column label="小计" width="120">
          <template #default="{ row }">
            ¥{{ (row.price * row.quantity).toFixed(2) }}
          </template>
        </el-table-column>
        
        <el-table-column label="操作" width="120">
          <template #default="{ row }">
            <el-button
              type="danger"
              size="small"
              @click="removeItem(row.id)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <div class="cart-footer">
        <div class="total">
          总计：<span class="price">¥{{ cartStore.total.toFixed(2) }}</span>
        </div>
        <el-button type="primary" size="large" @click="checkout">
          结算
        </el-button>
      </div>
    </el-card>
    
    <el-empty v-else description="购物车是空的">
      <el-button type="primary" @click="$router.push('/products')">
        去购物
      </el-button>
    </el-empty>
  </div>
</template>

<script setup>
import { onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useCartStore } from '../stores/cart'
import { ElMessageBox, ElMessage } from 'element-plus'

const router = useRouter()
const cartStore = useCartStore()

onMounted(async () => {
  await cartStore.fetchCart()
})

const updateQuantity = async (itemId, quantity) => {
  await cartStore.updateQuantity(itemId, quantity)
}

const removeItem = async (itemId) => {
  try {
    await ElMessageBox.confirm('确定要删除这个商品吗？', '提示', {
      type: 'warning'
    })
    await cartStore.removeItem(itemId)
    ElMessage.success('删除成功')
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('删除失败，请重试')
    }
  }
}

const clearCart = async () => {
  try {
    await ElMessageBox.confirm('确定要清空购物车吗？', '提示', {
      type: 'warning'
    })
    await cartStore.clearCart()
    ElMessage.success('购物车已清空')
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('操作失败，请重试')
    }
  }
}

const checkout = () => {
  router.push('/checkout')
}
</script>

<style scoped>
.cart-container {
  padding: 20px;
}

.cart-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.product-info {
  display: flex;
  align-items: center;
  gap: 20px;
}

.product-image {
  width: 80px;
  height: 80px;
  object-fit: cover;
  border-radius: 4px;
}

.product-details {
  flex: 1;
}

.product-description {
  color: #666;
  margin-top: 5px;
  font-size: 14px;
}

.cart-footer {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 20px;
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid #eee;
}

.total {
  font-size: 18px;
}

.price {
  color: #f56c6c;
  font-size: 24px;
  font-weight: bold;
}
</style> 